<!DOCTYPE html>

<html>
<head>
  <title>transactions.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="arkio.css" />
</head>
<body>
  
    <div id="title">
         <h1>transactions.js</h1>
         <img src="https://ark.io/wp-content/uploads/2016/10/ark-normal.png">
        <hr>
    </div>
  
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page_wrapper">
            <div id="jump_page">
              
                
                <a class="source" href="README.html">
                  README.md
                </a>
              
                
                <a class="source" href="app.html">
                  app.js
                </a>
              
                
                <a class="source" href="accounts.html">
                  accounts.js
                </a>
              
                
                <a class="source" href="blockchain.html">
                  blockchain.js
                </a>
              
                
                <a class="source" href="blocks.html">
                  blocks.js
                </a>
              
                
                <a class="source" href="delegates.html">
                  delegates.js
                </a>
              
                
                <a class="source" href="loader.html">
                  loader.js
                </a>
              
                
                <a class="source" href="multisignatures.html">
                  multisignatures.js
                </a>
              
                
                <a class="source" href="nodeManager.html">
                  nodeManager.js
                </a>
              
                
                <a class="source" href="peers.html">
                  peers.js
                </a>
              
                
                <a class="source" href="rounds.html">
                  rounds.js
                </a>
              
                
                <a class="source" href="server.html">
                  server.js
                </a>
              
                
                <a class="source" href="signatures.html">
                  signatures.js
                </a>
              
                
                <a class="source" href="system.html">
                  system.js
                </a>
              
                
                <a class="source" href="transactionPool.html">
                  transactionPool.js
                </a>
              
                
                <a class="source" href="transactions.html">
                  transactions.js
                </a>
              
                
                <a class="source" href="transport.html">
                  transport.js
                </a>
              
            </div>
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
        
        <li id="section-1">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-meta">'use strict'</span>;

<span class="hljs-keyword">var</span> <span class="hljs-keyword">async</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async'</span>);
<span class="hljs-keyword">var</span> ByteBuffer = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bytebuffer'</span>);
<span class="hljs-keyword">var</span> constants = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/constants.js'</span>);
<span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
<span class="hljs-keyword">var</span> extend = <span class="hljs-built_in">require</span>(<span class="hljs-string">'extend'</span>);
<span class="hljs-keyword">var</span> genesisblock = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">var</span> OrderBy = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/orderBy.js'</span>);
<span class="hljs-keyword">var</span> Router = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/router.js'</span>);
<span class="hljs-keyword">var</span> schema = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../schema/transactions.js'</span>);
<span class="hljs-keyword">var</span> slots = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/slots.js'</span>);
<span class="hljs-keyword">var</span> sql = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../sql/transactions.js'</span>);
<span class="hljs-keyword">var</span> Transfer = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../logic/transfer.js'</span>);
<span class="hljs-keyword">var</span> transactionTypes = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/transactionTypes.js'</span>);</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Private fields</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> modules, library, self, __private = {}, shared = {};

__private.assetTypes = {};</pre></div></div>
            
        </li>
        
        
        <li id="section-3">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Constructor</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Transactions</span> (<span class="hljs-params">cb, scope</span>) </span>{
	library = scope;
	genesisblock = library.genesisblock;
	self = <span class="hljs-keyword">this</span>;

	__private.assetTypes[transactionTypes.SEND] = library.logic.transaction.attachAssetType(
		transactionTypes.SEND, <span class="hljs-keyword">new</span> Transfer()
	);

	setImmediate(cb, <span class="hljs-literal">null</span>, self);
}</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Private methods</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>__private.attachApi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
	<span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> Router();

	router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
		<span class="hljs-keyword">if</span> (modules) { <span class="hljs-keyword">return</span> next(); }
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'Blockchain is loading'</span>});
	});

	router.map(shared, {
		<span class="hljs-string">'get /'</span>: <span class="hljs-string">'getTransactions'</span>,
		<span class="hljs-string">'get /get'</span>: <span class="hljs-string">'getTransaction'</span>,
		<span class="hljs-string">'get /unconfirmed/get'</span>: <span class="hljs-string">'getUnconfirmedTransaction'</span>,
		<span class="hljs-string">'get /unconfirmed'</span>: <span class="hljs-string">'getUnconfirmedTransactions'</span>,
		<span class="hljs-string">'put /'</span>: <span class="hljs-string">'addTransactions'</span>
	});

	router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'API endpoint not found'</span>});
	});

	library.network.app.use(<span class="hljs-string">'/api/transactions'</span>, router);
	library.network.app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, req, res, next</span>) </span>{
		<span class="hljs-keyword">if</span> (!err) { <span class="hljs-keyword">return</span> next(); }
		library.logger.error(<span class="hljs-string">'API error '</span> + req.url, err);
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'API error'</span>, <span class="hljs-attr">message</span>: err.message});
	});
};

__private.list = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">filter, cb</span>) </span>{
	<span class="hljs-keyword">var</span> sortFields = sql.sortFields;
	<span class="hljs-keyword">var</span> params = {}, where = [], owner = <span class="hljs-string">''</span>;

	<span class="hljs-keyword">if</span> (filter.blockId) {
		where.push(<span class="hljs-string">'"blockId" = ${blockId}'</span>);
		params.blockId = filter.blockId;
	}

	<span class="hljs-keyword">if</span> (filter.senderPublicKey) {
		where.push(<span class="hljs-string">'"senderPublicKey"::bytea = ${senderPublicKey}'</span>);
		params.senderPublicKey = filter.senderPublicKey;
	}

	<span class="hljs-keyword">if</span> (filter.senderId) {
		where.push(<span class="hljs-string">'"senderId" = ${senderId}'</span>);
		params.senderId = filter.senderId;
	}

	<span class="hljs-keyword">if</span> (filter.recipientId) {
		where.push(<span class="hljs-string">'"recipientId" = ${recipientId}'</span>);
		params.recipientId = filter.recipientId;
	}

	<span class="hljs-keyword">if</span> (filter.ownerAddress &amp;&amp; filter.ownerPublicKey) {
		owner = <span class="hljs-string">'("senderPublicKey"::bytea = ${ownerPublicKey} OR "recipientId" = ${ownerAddress})'</span>;
		params.ownerPublicKey = filter.ownerPublicKey;
		params.ownerAddress = filter.ownerAddress;
	}

	<span class="hljs-keyword">if</span> (filter.type &gt;= <span class="hljs-number">0</span>) {
		where.push(<span class="hljs-string">'"type" = ${type}'</span>);
		params.type = filter.type;
	}

	<span class="hljs-keyword">if</span> (!filter.limit) {
		params.limit = constants.maxTxsPerBlock;
	} <span class="hljs-keyword">else</span> {
		params.limit = <span class="hljs-built_in">Math</span>.abs(filter.limit);
	}

	<span class="hljs-keyword">if</span> (!filter.offset) {
		params.offset = <span class="hljs-number">0</span>;
	} <span class="hljs-keyword">else</span> {
		params.offset = <span class="hljs-built_in">Math</span>.abs(filter.offset);
	}

	<span class="hljs-keyword">if</span> (params.limit &gt; constants.maxTxsPerBlock) {
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid limit. Maximum is '</span>+constants.maxTxsPerBlock);
	}

	<span class="hljs-keyword">var</span> orderBy = OrderBy(
		filter.orderBy, {
			<span class="hljs-attr">sortFields</span>: sql.sortFields,
			<span class="hljs-attr">fieldPrefix</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sortField</span>) </span>{
				<span class="hljs-keyword">if</span> ([<span class="hljs-string">'height'</span>, <span class="hljs-string">'blockId'</span>, <span class="hljs-string">'confirmations'</span>].indexOf(sortField) &gt; <span class="hljs-number">-1</span>) {
					<span class="hljs-keyword">return</span> sortField;
				} <span class="hljs-keyword">else</span> {
					<span class="hljs-keyword">return</span> sortField;
				}
			}
		}
	);

	<span class="hljs-keyword">if</span> (orderBy.error) {
		<span class="hljs-keyword">return</span> setImmediate(cb, orderBy.error);
	}

	library.db.query(sql.countList({
		<span class="hljs-attr">where</span>: where,
		<span class="hljs-attr">owner</span>: owner
	}), params).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">rows</span>) </span>{
		<span class="hljs-keyword">var</span> count = rows.length ? rows[<span class="hljs-number">0</span>].count : <span class="hljs-number">0</span>;

		library.db.query(sql.list({
			<span class="hljs-attr">where</span>: where,
			<span class="hljs-attr">owner</span>: owner,
			<span class="hljs-attr">sortField</span>: orderBy.sortField,
			<span class="hljs-attr">sortMethod</span>: orderBy.sortMethod
		}), params).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">rows</span>) </span>{
			<span class="hljs-keyword">var</span> transactions = [];
			
			<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; rows.length; i++) {
				transactions.push(library.logic.transaction.dbRead(rows[i]));
			}

			<span class="hljs-keyword">var</span> data = {
				<span class="hljs-attr">transactions</span>: transactions,
				<span class="hljs-attr">count</span>: count
			};

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, data);
		}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
			library.logger.error(<span class="hljs-string">"stack"</span>, err.stack);
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transactions#list error'</span>);
		});
	}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		library.logger.error(<span class="hljs-string">"stack"</span>, err.stack);
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transactions#list error'</span>);
	});
};

__private.getById = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">id, cb</span>) </span>{
	library.db.query(sql.getById, {<span class="hljs-attr">id</span>: id}).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">rows</span>) </span>{
		<span class="hljs-keyword">if</span> (!rows.length) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transaction not found: '</span> + id);
		}

		<span class="hljs-keyword">var</span> transaction = library.logic.transaction.dbRead(rows[<span class="hljs-number">0</span>]);

		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, transaction);
	}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		library.logger.error(<span class="hljs-string">"stack"</span>, err);
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transactions#getById error'</span>);
	});
};

__private.getVotesById = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transaction, cb</span>) </span>{
	library.db.query(sql.getVotesById, {<span class="hljs-attr">id</span>: transaction.id}).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">rows</span>) </span>{
		<span class="hljs-keyword">if</span> (!rows.length) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transaction not found: '</span> + id);
		}

		<span class="hljs-keyword">var</span> votes = rows[<span class="hljs-number">0</span>].votes.split(<span class="hljs-string">','</span>);
		<span class="hljs-keyword">var</span> added = [];
		<span class="hljs-keyword">var</span> deleted = [];

		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; votes.length; i++) {
			<span class="hljs-keyword">if</span> (votes[i].substring(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>) == <span class="hljs-string">"+"</span>) {
				added.push (votes[i].substring(<span class="hljs-number">1</span>));
			} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (votes[i].substring(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>) == <span class="hljs-string">"-"</span>) {
				deleted.push (votes[i].substring(<span class="hljs-number">1</span>));
			}
		}

		transaction.votes = {<span class="hljs-attr">added</span>: added, <span class="hljs-attr">deleted</span>: deleted};

		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, transaction);
	}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		library.logger.error(<span class="hljs-string">"stack"</span>, err.stack);
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transactions#getVotesById error'</span>);
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Public methods</p>

            </div>
            
        </li>
        
        
        <li id="section-6">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p><strong>API</strong> <code>apply</code></p>

            </div>
            
        </li>
        
        
        <li id="section-7">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.apply = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transaction, block, cb</span>) </span>{
	library.transactionSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>)</span>{
		library.logger.debug(<span class="hljs-string">'Applying confirmed transaction'</span>, transaction.id);
		modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: transaction.senderPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, sender</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(eachSeriesCb, err);
			}
			library.logic.transaction.apply(transaction, block, sender, cb);
		});
	}, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p><strong>API</strong> <code>undo</code></p>

            </div>
            
        </li>
        
        
        <li id="section-9">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.undo = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transaction, block, cb</span>) </span>{
	library.transactionSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>)</span>{
		library.logger.debug(<span class="hljs-string">'Undoing confirmed transaction'</span>, transaction.id);
		modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: transaction.senderPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, sender</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(eachSeriesCb, err);
			}
			library.logic.transaction.undo(transaction, block, sender, cb);
		});
	}, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p><strong>API</strong> <code>applyUnconfirmed</code></p>

            </div>
            
        </li>
        
        
        <li id="section-11">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.applyUnconfirmed = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transaction, cb</span>) </span>{
	modules.accounts.setAccountAndGet({<span class="hljs-attr">publicKey</span>: transaction.senderPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, sender</span>) </span>{
		<span class="hljs-keyword">if</span> (!sender &amp;&amp; transaction.blockId !== genesisblock.block.id) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid block id'</span>);
		} <span class="hljs-keyword">else</span> {
			library.transactionSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>)</span>{
				library.logger.debug(<span class="hljs-string">'Applying unconfirmed transaction'</span>, transaction.id);
				<span class="hljs-keyword">if</span> (transaction.requesterPublicKey) {
					modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: transaction.requesterPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, requester</span>) </span>{
						<span class="hljs-keyword">if</span> (err) {
							<span class="hljs-keyword">return</span> setImmediate(cb, err);
						}

						<span class="hljs-keyword">if</span> (!requester) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Requester not found'</span>);
						}

						library.logic.transaction.applyUnconfirmed(transaction, sender, requester, cb);
					});
				} <span class="hljs-keyword">else</span> {
					library.logic.transaction.applyUnconfirmed(transaction, sender, cb);
				}
			}, cb);
		}
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p><strong>API</strong> <code>undoUnconfirmed</code></p>

            </div>
            
        </li>
        
        
        <li id="section-13">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.undoUnconfirmed = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">transaction, cb</span>) </span>{
	library.transactionSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>)</span>{
		library.logger.debug(<span class="hljs-string">'Undoing unconfirmed transaction'</span>, transaction.id);
		modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: transaction.senderPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, sender</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}
			library.logic.transaction.undoUnconfirmed(transaction, sender, cb);
		});
	}, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Events</p>
<p><strong>EVENT</strong> <code>onBind</code></p>

            </div>
            
        </li>
        
        
        <li id="section-15">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.onBind = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">scope</span>) </span>{
	modules = scope;

	__private.assetTypes[transactionTypes.SEND].bind({
		<span class="hljs-attr">modules</span>: modules, <span class="hljs-attr">library</span>: library
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p><strong>EVENT</strong> <code>onAttachPublicApi</code></p>

            </div>
            
        </li>
        
        
        <li id="section-17">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.onAttachPublicApi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
 	__private.attachApi();
};</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p><strong>EVENT</strong> <code>onPeersReady</code></p>

            </div>
            
        </li>
        
        
        <li id="section-19">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Transactions.prototype.onPeersReady = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
};</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>Shared</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>shared.getTransactions = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getTransactions, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		__private.list(req.body, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, data</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Failed to get transactions: '</span> + err);
			}

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transactions</span>: data.transactions, <span class="hljs-attr">count</span>: data.count});
		});
	});
};

shared.getTransaction = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getTransaction, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		__private.getById(req.body.id, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, transaction</span>) </span>{
			<span class="hljs-keyword">if</span> (!transaction || err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transaction not found'</span>);
			}
			<span class="hljs-keyword">if</span> (transaction.type == <span class="hljs-number">3</span>) {
				__private.getVotesById(transaction, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, transaction</span>) </span>{
					<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transaction</span>: transaction});
				});
			} <span class="hljs-keyword">else</span> {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transaction</span>: transaction});
			}
		});
	});
};

shared.getUnconfirmedTransaction = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getUnconfirmedTransaction, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> unconfirmedTransaction = modules.transactionPool.getUnconfirmedTransaction(req.body.id);

		<span class="hljs-keyword">if</span> (!unconfirmedTransaction) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Transaction not found'</span>);
		}

		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transaction</span>: unconfirmedTransaction});
	});
};

shared.getUnconfirmedTransactions = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getUnconfirmedTransactions, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> transactions = modules.transactionPool.getUnconfirmedTransactionList(<span class="hljs-literal">true</span>);
		<span class="hljs-keyword">var</span> i, toSend = [];

		<span class="hljs-keyword">if</span> (req.body.senderPublicKey || req.body.address) {
			<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; transactions.length; i++) {
				<span class="hljs-keyword">if</span> (transactions[i].senderPublicKey === req.body.senderPublicKey || transactions[i].recipientId === req.body.address) {
					toSend.push(transactions[i]);
				}
			}
		} <span class="hljs-keyword">else</span> {
			<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; transactions.length; i++) {
				toSend.push(transactions[i]);
			}
		}

		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transactions</span>: toSend});
	});
};

shared.addTransactions = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.addTransactions, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> keypair = library.ed.makeKeypair(req.body.secret);

		<span class="hljs-keyword">if</span> (req.body.publicKey) {
			<span class="hljs-keyword">if</span> (keypair.publicKey.toString(<span class="hljs-string">'hex'</span>) !== req.body.publicKey) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid passphrase'</span>);
			}
		}

		<span class="hljs-keyword">var</span> query = { <span class="hljs-attr">address</span>: req.body.recipientId };

		library.balancesSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
			modules.accounts.getAccount(query, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, recipient</span>) </span>{
				<span class="hljs-keyword">if</span> (err) {
					<span class="hljs-keyword">return</span> setImmediate(cb, err);
				}

				<span class="hljs-keyword">var</span> recipientId = recipient ? recipient.address : req.body.recipientId;

				<span class="hljs-keyword">if</span> (!recipientId) {
					<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid recipient'</span>);
				}

				<span class="hljs-keyword">if</span> (req.body.multisigAccountPublicKey &amp;&amp; req.body.multisigAccountPublicKey !== keypair.publicKey.toString(<span class="hljs-string">'hex'</span>)) {
					modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: req.body.multisigAccountPublicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
						<span class="hljs-keyword">if</span> (err) {
							<span class="hljs-keyword">return</span> setImmediate(cb, err);
						}

						<span class="hljs-keyword">if</span> (!account || !account.publicKey) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Multisignature account not found'</span>);
						}

						<span class="hljs-keyword">if</span> (!<span class="hljs-built_in">Array</span>.isArray(account.multisignatures)) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account does not have multisignatures enabled'</span>);
						}

						<span class="hljs-keyword">if</span> (account.multisignatures.indexOf(keypair.publicKey.toString(<span class="hljs-string">'hex'</span>)) &lt; <span class="hljs-number">0</span>) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account does not belong to multisignature group'</span>);
						}

						modules.accounts.getAccount({<span class="hljs-attr">publicKey</span>: keypair.publicKey}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, requester</span>) </span>{
							<span class="hljs-keyword">if</span> (err) {
								<span class="hljs-keyword">return</span> setImmediate(cb, err);
							}

							<span class="hljs-keyword">if</span> (!requester || !requester.publicKey) {
								<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Requester not found'</span>);
							}

							<span class="hljs-keyword">if</span> (requester.secondSignature &amp;&amp; !req.body.secondSecret) {
								<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Missing requester second passphrase'</span>);
							}

							<span class="hljs-keyword">if</span> (requester.publicKey === account.publicKey) {
								<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid requester public key'</span>);
							}

							<span class="hljs-keyword">var</span> secondKeypair = <span class="hljs-literal">null</span>;

							<span class="hljs-keyword">if</span> (requester.secondSignature) {
								secondKeypair = library.ed.makeKeypair(req.body.secondSecret);
							}

							<span class="hljs-keyword">var</span> transaction;

							<span class="hljs-keyword">try</span> {
								transaction = library.logic.transaction.create({
									<span class="hljs-attr">type</span>: transactionTypes.SEND,
									<span class="hljs-attr">amount</span>: req.body.amount,
									<span class="hljs-attr">sender</span>: account,
									<span class="hljs-attr">recipientId</span>: recipientId,
									<span class="hljs-attr">keypair</span>: keypair,
									<span class="hljs-attr">requester</span>: keypair,
									<span class="hljs-attr">secondKeypair</span>: secondKeypair
								});

								transaction.id=library.logic.transaction.getId(transaction);

								

							} <span class="hljs-keyword">catch</span> (e) {
								<span class="hljs-keyword">return</span> setImmediate(cb, e.toString());
							}

							library.bus.message(<span class="hljs-string">"transactionsReceived"</span>, [transaction], <span class="hljs-string">"api"</span>, cb);
						});
					});
				} <span class="hljs-keyword">else</span> {
					modules.accounts.setAccountAndGet({<span class="hljs-attr">publicKey</span>: keypair.publicKey.toString(<span class="hljs-string">'hex'</span>)}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
						<span class="hljs-keyword">if</span> (err) {
							<span class="hljs-keyword">return</span> setImmediate(cb, err);
						}

						<span class="hljs-keyword">if</span> (!account || !account.publicKey) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account not found'</span>);
						}

						<span class="hljs-keyword">if</span> (account.secondSignature &amp;&amp; !req.body.secondSecret) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Missing second passphrase'</span>);
						}

						<span class="hljs-keyword">var</span> secondKeypair = <span class="hljs-literal">null</span>;

						<span class="hljs-keyword">if</span> (account.secondSignature) {
							secondKeypair = library.ed.makeKeypair(req.body.secondSecret);
						}

						<span class="hljs-keyword">var</span> transaction;

						<span class="hljs-keyword">try</span> {
							transaction = library.logic.transaction.create({
								<span class="hljs-attr">type</span>: transactionTypes.SEND,
								<span class="hljs-attr">amount</span>: req.body.amount,
								<span class="hljs-attr">sender</span>: account,
								<span class="hljs-attr">vendorField</span>: req.body.vendorField,
								<span class="hljs-attr">recipientId</span>: recipientId,
								<span class="hljs-attr">keypair</span>: keypair,
								<span class="hljs-attr">secondKeypair</span>: secondKeypair
							});

							transaction.id=library.logic.transaction.getId(transaction);

						} <span class="hljs-keyword">catch</span> (e) {
							<span class="hljs-keyword">return</span> setImmediate(cb, e.toString());
						}

						library.bus.message(<span class="hljs-string">"transactionsReceived"</span>, [transaction], <span class="hljs-string">"api"</span>, cb);
					});
				}
			});
		}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, transaction</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transactionId</span>: transaction[<span class="hljs-number">0</span>].id});
		});
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>Export</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-built_in">module</span>.exports = Transactions;</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
